<template>
  <el-container class="homeContainer">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" />
        <span>电商后台管理系统</span>
      </div>
      <el-button @click="goBack" type="info">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="collapseFlag ? '68px' : '200px'">
        <div class="toggleMenu" @click="collapseIs">|||</div>
        <el-menu background-color="#333744" :unique-opened="true" text-color="#fff"
          :collapse-transition="false" :collapse="collapseFlag" :router="true"
          :default-active="activePath" active-text-color="#419efb">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模版区域 -->
            <template slot="title">
              <!-- 图标区域 -->
              <i :class="iconList[item.id]"></i>
              <!-- 文本区域 -->
              <span>{{ item.authName }}</span>
            </template>
            <!--  二级菜单 -->
            <el-menu-item @click="aetActive('/' + subItem.path)" :index="'/' + subItem.path"
              v-for="subItem in item.children" :key="subItem.id">
              <template slot="title">
                <!-- 图标区域 -->
                <i class="el-icon-menu"></i>
                <!-- 文本区域 -->
                <span>{{ subItem.authName }}</span>
              </template></el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <transition appear>
          <router-view></router-view>
        </transition>
      </el-main>
    </el-container>
  </el-container>
</template>

<script lang="ts">
export default {
  data() {
    return {
      menuList: [],
      iconList: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      collapseFlag: false,
      activePath: ''
    }
  },
  created() {
    this.getMenuList(),
      (this.activePath = window.sessionStorage.getItem('activePath'))
  },
  methods: {
    goBack() {
      this.$router.push('/login')
      const tokenStr = window.sessionStorage.getItem('token')
    },
    async getMenuList() {
      //获取所有表单数据
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status != 200) return this.$message.err(res.meta.msg)

      this.menuList = res.data
    },
    collapseIs() {
      this.collapseFlag = !this.collapseFlag
    },
    aetActive(activePath) {
      console.log(activePath)
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.homeContainer {
  height: 100%;
}
.el-header {
  background-color: #373d42;
  display: flex;
  justify-content: space-between;
  padding-left: 0px;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-container {
  background-color: #333744;
  .el-menu {
    border-right: 0;
    user-select: none;
  }
}
.el-main {
  background-color: #eaedf2;
}
.iconfont {
  margin-right: 10px;
}
.toggleMenu {
  width: 100%;
  height: 30px;
  background-color: #4a5064;
  font-size: 10px;
  line-height: 30px;
  text-align: center;
  color: #fff;
  letter-spacing: 2px;
  font-weight: bolder;
  cursor: pointer;
  user-select: none;
}
//  动画
.v-enter {
  transform: translateX(20px);
  opacity: 0;
}
.v-enter-active {
  transition: all 1s ease;
}
.v-enter-to {
  transform: translateX(0);
  opacity: 1;
}
</style>
